package Day008_List_Collections_set.Demo02_ArrayList集合;

//创建一个存储学生对象的集合,遍历 姓名年龄录入
//1.创建学生类
//2.创建集合
//3.创建3个学生对象
//4.添加
//5.遍历

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayList集合遍历学生类1 {
    public static void main(String[] args) {
        //2.创建集合
        ArrayList<students> list = new ArrayList<>();
        //调用方法一次,添加一个学生
        xxx(list);
        xxx(list);
        xxx(list);

        //4.遍历
        for (int i = 0; i < list.size(); i++) {
            //i=0,1,2
            //定义变量保存地址,//每个创建对象都会有个新地址
            students s = list.get(i);
            System.out.println(s.getName() + " " + s.getAge());
        }
    }

    //3.定义方法
    public static void xxx(ArrayList<students> list){
        //3.1键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("学生姓名:");
        String name = sc.next();
        System.out.println("学生年龄:");
        int age = sc.nextInt();
        //3.2创建学生对象
        students s = new students(name,age); //每个创建对象都会有个新地址
        //3.3添加到集合
        list.add(s); //添加的是创建对象的地址
    }
}

//----------------------------------------------------------------------------------------------------------------------

//自己通过class定义的类,属于引用数据类型
//1.定义学生类
class students
{
    private String name;
    private int age;

    public students() {

    }

    public students(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

